package com.tengju.bff.interfaces.app.user.assembler;


import com.google.common.collect.Lists;
import com.tengju.bff.interfaces.app.user.vo.RangeVO;
import com.tengju.bff.interfaces.app.user.vo.ReportDealerDominationNewUserVO;
import com.tengju.bff.interfaces.app.user.vo.ReportDealerNewUserVO;
import com.tengju.data.domain.model.user.RangeNewUserEnum;
import com.tengju.data.domain.model.user.ReportDealerDominationNewUserInfo;
import com.tengju.data.domain.model.user.ReportDealerNewUserInfo;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.data.domain.shared.Month;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class ManageCentreDealerNewAssembler {

    public static List<ReportDealerNewUserVO> toNewUserVOList(List<ReportDealerNewUserInfo> infoList,
                                                              Integer month,
                                                              Map<Integer,String> associationTitleMap,
                                                              Map<Integer,Integer> associationOrderMap) {

        List<ReportDealerNewUserVO> voList = Lists.newArrayList();

        //黑钻新增
        ReportDealerNewUserVO level4VO = new ReportDealerNewUserVO();
        Long level4Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerNewUserInfo::getLevel4NewCnt)
                .sum();
        Map<String,Long> range4Map = infoList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ReportDealerNewUserInfo::getVisitDate, ReportDealerNewUserInfo::getLevel4NewCnt,(v1, v2) -> v1));
        level4VO.setTotal(String.valueOf(level4Total));
        level4VO.setTitle(associationTitleMap.get(RangeNewUserEnum.SVIP.getCode()));
        level4VO.setOrder(associationOrderMap.get(RangeNewUserEnum.SVIP.getCode()));
        level4VO.setList(getRangeCnt(range4Map,month));
        voList.add(level4VO);

        //白金新增
        ReportDealerNewUserVO level3VO = new ReportDealerNewUserVO();
        Long level3Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerNewUserInfo::getLevel3NewCnt)
                .sum();
        Map<String,Long> range3Map = infoList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ReportDealerNewUserInfo::getVisitDate, ReportDealerNewUserInfo::getLevel3NewCnt,(v1, v2) -> v1));
        level3VO.setTotal(String.valueOf(level3Total));
        level3VO.setTitle(associationTitleMap.get(RangeNewUserEnum.VVIP.getCode()));
        level3VO.setOrder(associationOrderMap.get(RangeNewUserEnum.VVIP.getCode()));
        level3VO.setList(getRangeCnt(range3Map,month));
        voList.add(level3VO);

        // 粉卡新增
        ReportDealerNewUserVO level2VO = new ReportDealerNewUserVO();
        Long level2Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerNewUserInfo::getLevel2NewCnt)
                .sum();
        Map<String,Long> range2Map = infoList.stream()
                .collect(Collectors.toMap(ReportDealerNewUserInfo::getVisitDate, ReportDealerNewUserInfo::getLevel2NewCnt,(v1, v2) -> v1));
        level2VO.setTotal(String.valueOf(level2Total));
        level2VO.setTitle(associationTitleMap.get(RangeNewUserEnum.VIP.getCode()));
        level2VO.setOrder(associationOrderMap.get(RangeNewUserEnum.VIP.getCode()));
        level2VO.setList(getRangeCnt(range2Map,month));
        voList.add(level2VO);

        // 用户新增
        ReportDealerNewUserVO level1VO = new ReportDealerNewUserVO();
        Long level1Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerNewUserInfo::getLevel1NewCnt)
                .sum();
        Map<String,Long> range1Map = infoList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ReportDealerNewUserInfo::getVisitDate, ReportDealerNewUserInfo::getLevel1NewCnt,(v1, v2) -> v1));
        level1VO.setTotal(String.valueOf(level1Total));
        level1VO.setTitle(associationTitleMap.get(RangeNewUserEnum.NORMAL.getCode()));
        level1VO.setOrder(associationOrderMap.get(RangeNewUserEnum.NORMAL.getCode()));
        level1VO.setList(getRangeCnt(range1Map,month));
        voList.add(level1VO);

        return orderAssociation(voList,associationTitleMap);
    }

    public static List<ReportDealerDominationNewUserVO> toDominationNewUserVOList(List<ReportDealerDominationNewUserInfo> infoList,
                                                                                  Integer year,
                                                                                  Map<Integer,String> dominationTitleMap,
                                                                                  Map<Integer,Integer> dominationOrderMap) {

        List<ReportDealerDominationNewUserVO> voList = Lists.newArrayList();

        // 经销商新增
        ReportDealerDominationNewUserVO level5VO = new ReportDealerDominationNewUserVO();
        Long level5Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerDominationNewUserInfo::getLevel5NewCnt)
                .sum();
        Map<Integer,Long> range5Map = infoList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ReportDealerDominationNewUserInfo::getMonth, ReportDealerDominationNewUserInfo::getLevel5NewCnt,(v1, v2) -> v1));
        level5VO.setTotal(String.valueOf(level5Total));
        level5VO.setTitle(dominationTitleMap.get(RangeNewUserEnum.BLACK.getCode()));
        level5VO.setOrder(dominationOrderMap.get(RangeNewUserEnum.BLACK.getCode()));
        level5VO.setList(getDominationRangeCnt(range5Map,year));
        voList.add(level5VO);

        // 黑钻新增
        ReportDealerDominationNewUserVO level4VO = new ReportDealerDominationNewUserVO();
        Long level4Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerDominationNewUserInfo::getLevel4NewCnt)
                .sum();
        Map<Integer,Long> range4Map = infoList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ReportDealerDominationNewUserInfo::getMonth, ReportDealerDominationNewUserInfo::getLevel4NewCnt,(v1, v2) -> v1));
        level4VO.setTotal(String.valueOf(level4Total));
        level4VO.setTitle(dominationTitleMap.get(RangeNewUserEnum.SVIP.getCode()));
        level4VO.setOrder(dominationOrderMap.get(RangeNewUserEnum.SVIP.getCode()));
        level4VO.setList(getDominationRangeCnt(range4Map,year));
        voList.add(level4VO);

        // 白金新增
        ReportDealerDominationNewUserVO level3VO = new ReportDealerDominationNewUserVO();
        Long level3Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerDominationNewUserInfo::getLevel3NewCnt)
                .sum();
        Map<Integer,Long> range3Map = infoList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ReportDealerDominationNewUserInfo::getMonth, ReportDealerDominationNewUserInfo::getLevel3NewCnt,(v1, v2) -> v1));
        level3VO.setTotal(String.valueOf(level3Total));
        level3VO.setTitle(dominationTitleMap.get(RangeNewUserEnum.VVIP.getCode()));
        level3VO.setOrder(dominationOrderMap.get(RangeNewUserEnum.VVIP.getCode()));
        level3VO.setList(getDominationRangeCnt(range3Map,year));
        voList.add(level3VO);

        // 粉卡新增
        ReportDealerDominationNewUserVO level2VO = new ReportDealerDominationNewUserVO();
        Long level2Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerDominationNewUserInfo::getLevel2NewCnt)
                .sum();
        Map<Integer,Long> range2Map = infoList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ReportDealerDominationNewUserInfo::getMonth, ReportDealerDominationNewUserInfo::getLevel2NewCnt,(v1, v2) -> v1));
        level2VO.setTotal(String.valueOf(level2Total));
        level2VO.setTitle(dominationTitleMap.get(RangeNewUserEnum.VIP.getCode()));
        level2VO.setOrder(dominationOrderMap.get(RangeNewUserEnum.VIP.getCode()));
        level2VO.setList(getDominationRangeCnt(range2Map,year));
        voList.add(level2VO);

        // 用户新增
        ReportDealerDominationNewUserVO level1VO = new ReportDealerDominationNewUserVO();
        Long level1Total = infoList.stream()
                .filter(Objects::nonNull)
                .mapToLong(ReportDealerDominationNewUserInfo::getLevel1NewCnt)
                .sum();
        Map<Integer,Long> range1Map = infoList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ReportDealerDominationNewUserInfo::getMonth, ReportDealerDominationNewUserInfo::getLevel1NewCnt,(v1, v2) -> v1));
        level1VO.setTotal(String.valueOf(level1Total));
        level1VO.setTitle(dominationTitleMap.get(RangeNewUserEnum.NORMAL.getCode()));
        level1VO.setOrder(dominationOrderMap.get(RangeNewUserEnum.NORMAL.getCode()));
        level1VO.setList(getDominationRangeCnt(range1Map,year));
        voList.add(level1VO);

        return orderDomination(voList,dominationTitleMap);

    }


    /**
     * 获取天区间新增数
     * @param rangeMap 源数据
     * @param month yyyyMM
     * @return java.util.List<java.lang.Long>
     */
    public static List<RangeVO> getRangeCnt(Map<String,Long> rangeMap,Integer month) {
        LocalDate startMonthDate = new Month(month).getStartDate();
        List<String> rangeDateList = Lists.newArrayList();
        for (int i = 0; i < startMonthDate.lengthOfMonth(); i++) {
            rangeDateList.add(startMonthDate.plusDays(i).toString());
        }

        List<RangeVO> rangeCntList = Lists.newArrayList();
        rangeDateList.forEach(rangeDate -> {
            RangeVO rangeVO = new RangeVO(
                    String.format("%s日",Integer.parseInt(rangeDate.substring(8))),
                    rangeMap.get(rangeDate) == null ? "0" : String.valueOf(rangeMap.get(rangeDate))
            );

            rangeCntList.add(rangeVO);
        });
        return rangeCntList;

    }


    public static List<RangeVO> getDominationRangeCnt(Map<Integer,Long> rangeMap,Integer year) {
        LocalDateTime yearDate = DateUtil.format(year.toString(),DateUtil.DATE_YEAR_FORMAT);
        // 区间 12 个月
        List<Integer> rangeDateList = Lists.newArrayList();
        for (int i = 0; i < 12; i++) {
            rangeDateList.add(DateUtil.formatMonthInt(yearDate.plusMonths(i)));
        }

        List<RangeVO> rangeCntList = Lists.newArrayList();

        rangeDateList.forEach(rangeDate -> {
            RangeVO rangeVO = new RangeVO(
                    String.format("%s月",Integer.parseInt(rangeDate.toString().substring(4))),
                    rangeMap.get(rangeDate) == null ? "0" : String.valueOf(rangeMap.get(rangeDate))
            );

            rangeCntList.add(rangeVO);
        });

        return rangeCntList;
    }

    /***
     * 控制社群展示数量和排序
     */
    public static List<ReportDealerNewUserVO> orderAssociation(List<ReportDealerNewUserVO> voList,
                                                               Map<Integer,String> associationTitleMap) {

        List<ReportDealerNewUserVO> newUserVOList = Lists.newArrayList();
        if (associationTitleMap == null || associationTitleMap.isEmpty()) {
            return newUserVOList;
        }

        voList.forEach(vo -> {
            if (associationTitleMap.containsValue(vo.getTitle())) {
                newUserVOList.add(vo);
            }
        });

        // 排序
        newUserVOList.sort(Comparator.comparing(ReportDealerNewUserVO::getOrder));

        return newUserVOList;
    }

    public static List<ReportDealerDominationNewUserVO> orderDomination(List<ReportDealerDominationNewUserVO> voList,
                                                                        Map<Integer,String> dominationTitleMap) {

        List<ReportDealerDominationNewUserVO> dominationNewUserVOList = Lists.newArrayList();
        if (dominationTitleMap == null || dominationTitleMap.isEmpty()) {
            return dominationNewUserVOList;
        }

        voList.forEach(vo -> {
            if (dominationTitleMap.containsValue(vo.getTitle())) {
                dominationNewUserVOList.add(vo);
            }
        });
        dominationNewUserVOList.sort(Comparator.comparing(ReportDealerDominationNewUserVO::getOrder));

        return dominationNewUserVOList;

    }



}
